@echo off

rem 192.168.1.35
set NAS=zsludov
rem set NASPATH=public\Office\backup
rem set BCKDRIVE=Z:
set LOG="C:\Documents and Settings\New user\logoff.log"

rem shortcut.lnk Minimized, Icon: %SystemRoot%\system32\SHELL32.dll <POWEROFF>

set       BLOCKINPUT="C:\Documents and Settings\New user\Asztal\blockinput.exe"
rem blat.exe -installSMTP szepe.net COMPUTER@DOMAIN.TLD 5 25 - freemail@szepe.net freemail
set             BLAT="C:\Program Files\utl\blat.exe"
set         CCLEANER="C:\Documents and Settings\New user\Asztal\CCleaner.exe"
set       DEFRAGGLER="C:\Documents and Settings\New user\Asztal\df.exe"
set            ARECA="C:\Program Files\Areca\areca_cl.exe"
set            ERUNT="C:\Program Files\ERUNT\AUTOBACK.EXE"
set         NTREGOPT="C:\Program Files\ERUNT\NTREGOPT.EXE"

rem ------------------------------------------------------------------
set BLATOPTS="-" -to szepeviktor@t-online.hu -subject "[admin] %COMPUTERNAME% logoff szkript"
set CCLEANEROPTS="/AUTO"
set DEFRAGGLEROPTS="/QD"
set ARECAOPTS="backup -config"
set BCFGS="C:\Documents and Settings\New user\.areca\workspace\*.bcfg"
set ERUNTOPTS="%SystemRoot%\ERUNT\AutoBackup\#Date# /day:30"
set NTREGOPTOPTS="silent"
set DIESOUND="%SystemRoot%\Media\chord.wav"
set ERROR=-


rem echo ----------------------- felhasznaloi resz ---
:SETTING
color f1
title Karbantartas        levelcim: info@szepe.net
CALL :LOG "start: %~0"
rem au3: #NoTrayIcon
rem      BlockInput(1);
rem      Sleep(3600*1000);
rem no /WAIT
start "block input" /MIN %BLOCKINPUT%
set STATE=manual
rem only in XP Professinal
if EXIST "%SystemRoot%\system32\tasklist.exe" (
  tasklist /FI "IMAGENAME eq explorer.exe" | find /I "explorer.exe"
  if ERRORLEVEL 1 (
    rem logoff-ban vagyunk, mert nem fut az explorer
    set STATE=logoff
    )
  )

:DEPENDS
CALL :REGREAD "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
CALL :REGREAD "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\%RET%" "JavaHome"
set JAVA="%RET%\bin\java.exe"
if NOT EXIST %JAVA% (
  CALL :DIE "no java for Areca"
  )
java -version 2>&1 | find /I "java version" >> %LOG%

:KILLPROC
if EXIST "%SystemRoot%\system32\tskill.exe" (
  tskill outlook
  tskill msimn
  tskill wlmail
  tskill thunderbird
  tskill firefox
  tskill chrome
  tskill iexplore
  tskill opera
  )

:CLEAN
CALL :RUN "clean" %CCLEANER% %CCLEANEROPTS%

:NASSEARCH
ping -n 2 %NAS% >nul
if ERRORLEVEL 1 (
  echo --
  echo KEREM KAPCSOLJA BE A MENTESHEZ SZUKSEGES KESZULEKET
  echo.
  goto :NASSEARCH
  )

rem :MAPNAS
rem net use %BCKDRIVE% /d
rem net use %BCKDRIVE% \\%NAS%\%NASPATH% /persistent:NO
rem if ERRORLEVEL 1 CALL: DIE "cannot attach NAS"

:BACKUP
rem FIXME .bcfg-ket NAS-ra menteni
for %%C in (%BCFGS%) do (
  CALL :RUN "areca" %ARECA% %ARECAOPTS% "%%C"
  CALL :LOG "areca %%~nC Ret=%RET%"
  )

rem echo ----------------------- rendszergazdai resz ---

:ISADMIN
rem XP Pro: whoami /PRIV | find /I "SeSecurityPrivilege" >nul
reg query "HKU\S-1-5-19" >nul
if ERRORLEVEL 1 CALL :DIE "not admin"

:ERUNT
CALL :RUN "erunt" %ERUNT% %ERUNTOPTS%

:DEFRAG
CALL :RUN "defrag" %DEFRAGGLER% %SystemDrive% %DEFRAGGLEROPTS%

:NTREGOPT
CALL :RUN "ntregopt" %NTREGOPT% %NTREGOPTOPTS%

:SHUTDOWN
if [%STATE%]==[manual] shutdown -s -t 3 -c "logoff szkript" -f

CALL :LOG "end: %ERROR%"
exit 0








::-----------------------------------------------------------------------

:RUN
:: CALL :RUN "name" "path.exe" "arg1 arg2 arg3..."
if EXIST %2 (
  echo start %~1: %2 %~3 %4 %5 %6 %7 %8 %9
  start %1 /WAIT /MIN %2 %~3 %4 %5 %6 %7 %8 %9
  set RET=%ERRORLEVEL%
  ) else (
  set ERROR=%ERROR%__NOTFOUND-%~1
  set RET=99
  )
goto :EOF

:REGREAD
::CALL :REGREAD "<regkey>" "<valuename>"
:: ret: %RET%
::REG.EXE 2000/XP: delims=<TAB>  Vista,7: delims=<SPACE>
::FIXME value name contains space
set "RET="
for /F "tokens=2*" %%A in ('reg.exe QUERY %1 /v %2 2^>nul ^| find "REG_"') do set RET=%%B
goto :EOF

:REGREAD2
::CALL :REGREAD2 "<regkey>" "<value name>"
:: ret: %RET%
::value name HAS 1 space
set "RET="
for /F "tokens=3*" %%A in ('reg.exe QUERY %1 /v %2 2^>nul ^| find "REG_"') do set RET=%%B
goto :EOF

:LOG
::CALL :LOG "message text"
echo [%DATE% %TIME%] %~1 >>%LOG%
goto :EOF

:DIE
::CALL :DIE "error message"
CALL :LOG %1
echo ---
CALL :RUN "blat" %BLAT% %BLATOPTS% -body "error:%1  ERROR=%ERROR%"
CALL :RUN "chord" %DIESOUND%
exit 1
